﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.Events;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.BusinessEntities;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.BusinessObjects;

namespace SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.DataAccess
{
    public class CalendarOptions
    {
        #region Variables
        private DaySelectionCollection _SelectionCollection = null;
        private DateRangeCollection _DateRangeCollection = null;
        private XDocument xDoc = null;
        #endregion

        #region Event Variables
        public event OptionsLoadedHandler OptionsLoaded;
        #endregion

        #region Constructor
        public CalendarOptions()
        {
            _SelectionCollection = new DaySelectionCollection();
            _DateRangeCollection = new DateRangeCollection();
        }
        #endregion

        #region Events
        #region RequestXML_Completed
        private void RequestXML_Completed(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                xDoc = XDocument.Parse(e.Result);
                GetDaySelectionList();
            }
        }
        #endregion
        #endregion

        #region Methods
        #region RequestXML
        public void RequestXML(string CalendarType)
        {
            WebClient _Client = new WebClient();
            _Client.DownloadStringAsync(new Uri(CalendarType + "CalendarOptions.xml", UriKind.RelativeOrAbsolute));
            _Client.DownloadStringCompleted += RequestXML_Completed;
        }
        #endregion

        #region GetDaySelectionList
        private void GetDaySelectionList()
        {
            foreach (XElement _Item in xDoc.Element("DaySelection").Element("Values").Elements("Add"))
            {
                DaySelection _DaySelection = new DaySelection();
                _DaySelection.Value = Int32.Parse(_Item.Attribute("key").Value);
                _DaySelection.Desc = _Item.Attribute("value").Value;
                _DaySelection.Color = SLControlLibrary.Resources.Common.Utility.ConvertStringToColor(_Item.Attribute("color").Value);
                _DaySelection.HalfDay = bool.Parse(_Item.Attribute("halfday").Value);

                _SelectionCollection.Add(_DaySelection);
            }

            GetDateRangeList();
        }
        #endregion

        #region GetDateRangeList
        private void GetDateRangeList()
        {
            foreach (XElement _Item in xDoc.Element("DaySelection").Element("DateRange").Elements("Range"))
            {
                DateRange _DateRange = new DateRange();
                _DateRange.InDate = DateTime.Parse(_Item.Attribute("indate").Value);
                _DateRange.OutDate = DateTime.Parse(_Item.Attribute("outdate").Value);
                _DateRange.DaySelectionValue = Int32.Parse(_Item.Attribute("value").Value);

                _DateRangeCollection.Add(_DateRange);
            }

            OptionsLoadedEventArgs e = new OptionsLoadedEventArgs(_SelectionCollection, _DateRangeCollection);
            OptionsLoaded(this, e);
        }
        #endregion
        #endregion
    }
}
